﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SCALP.BuildDebug;
using FastColoredTextBoxNS;

namespace TestCompiler
{
    public partial class Form1 : Form
    {
        #region Text Box Styles
        TextStyle RegisterStyle = new TextStyle(Brushes.Blue, null, FontStyle.Bold);
        TextStyle CharStyle = new TextStyle(Brushes.DarkOrange, null, FontStyle.Regular);
        #endregion

        public Form1()
        {
            InitializeComponent();
        }
        private byte[] programMade;
        private DataTable errors = new DataTable();

        private void Form1_Load(object sender, EventArgs e)
        {
            errors.Clear();
            errors.Columns.Add("Message");
            errors.Columns.Add("Line Number");
            dataGridView1.DataSource = errors;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            string cs_array = "";
            this.Cursor = Cursors.WaitCursor;
            errors.Clear();
            try
            {
                programMade = SCALP.Compiler.Compile(textBox1.Text);

                if (programMade == null)
                {
                    throw new BuildErrorException("Unknown Error", 0);
                }
                else
                {
                    int n = 0;
                    for (int i = 0; i < programMade.Length; i++)
                    {
                        n++;
                        if (n != 6)
                        {
                            cs_array += (programMade[i].ToString() + ", ");
                        }
                        else
                        {
                            cs_array += (programMade[i].ToString() + ", \n");
                            n = 0;
                        }
                    }
                }
                DisplayBox_csarray dw = new DisplayBox_csarray(cs_array);
                dw.Show();
            }
            catch (BuildErrorException ex)
            {
                textBox1.BackColor = Color.Red;
                DataRow row = errors.NewRow();
                row["Message"] = ex.Message;
                row["Line Number"] = ex.onLineNum.ToString();
                errors.Rows.Add(row);
            }
            this.Cursor = Cursors.Default;
        }

        private void textBox1_TextChanged(object sender, FastColoredTextBoxNS.TextChangedEventArgs e)
        {
            if (textBox1.BackColor == Color.Red)
                textBox1.BackColor = Color.White;
            e.ChangedRange.ClearStyle(RegisterStyle, CharStyle);

            e.ChangedRange.SetStyle(CharStyle, "\'\\\\?[A-Za-z0-9]\'");
            e.ChangedRange.SetStyle(RegisterStyle, @" \b(PC|IP|SP|SS|AL|AH|BL|BH|CL|CH|A|B|C|X|Y)");
            
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            errors.Clear();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
            StreamWriter sw = new StreamWriter(File.OpenWrite(saveFileDialog1.FileName));
            sw.Write(textBox1.Text);
            sw.Close();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            StreamReader sr = new StreamReader(File.OpenRead(openFileDialog1.FileName));
            textBox1.Text = sr.ReadToEnd();
            sr.Close();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            try
            {
                programMade = SCALP.Compiler.Compile(textBox1.Text);
                saveBinary.ShowDialog();
                try
                {
                    FileStream bry = File.OpenWrite(saveBinary.FileName);
                    bry.Write(programMade, 0, programMade.Length);
                    bry.Close();
                }
                catch (ArgumentException ax)
                { // this is from the user abandoning the save 
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "File Save Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
            catch (BuildErrorException ex)
            {
                textBox1.BackColor = Color.Red;
                DataRow row = errors.NewRow();
                row["Message"] = ex.Message;
                row["Line Number"] = ex.onLineNum.ToString();
                errors.Rows.Add(row);
            }
        }

        private void firstWriteableMemoryPositionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (programMade != null)
            {
                MessageBox.Show("The first writable memory position is: " + (programMade.Length + 1).ToString(), "First Writeable Memory Position");
            }
            else
            {
                MessageBox.Show("The application must be compiled first", "First Writeable Memory Position", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }
    }
}
